<?php
/***************************************************************************
 *                               edit_pilot.php
 *                            -------------------
 *                       (APU) Automated Pilot Utility
 *   begin                : Saturday, Dec 18, 2004
 *   copyright            : (C) 2004 SF Games
 *   email                : techsupport@sf-games.com
 *
 *   Expects the following POST variable:
 *
 *            Pilot ID: pilot_id
 ***************************************************************************/

	$apus_root_path = '../include/';
	include($apus_root_path . 'common.php');
	include($apus_root_path . 'config.php');

	$bRedo    = false;
	$pilotId  = "";
	$bPilotId = false;

	if ( isset($_POST['pilot_id']) )
	{
		if ( strlen($_POST['pilot_id']) > 0 && strlen($_POST['pilot_id']) <= $MAX_ID_LENGTH )
		{
			$pilotId = trim($_POST['pilot_id']);
			$bPilotId = true;
		}
		else
		{
			$sErrorsArray[] = "The given pilot id is empty or exceeds the allowed length of " .$MAX_ID_LENGTH ."<br>";
			$bRedo = true;
		}
	}

	//
	// Connect to database
	//

	$db = mysql_connect($dbhost, $dbuname, $dbpass)
		or die("Could not connect to the SQL server<br>");
	mysql_select_db($dbname);

	//
	// Get the given Pilot ID information
	//

	$query  = "SELECT * FROM " .$table_prefix ."PILOTS WHERE PILOT_ID=\"" .$pilotId ."\"";
	$result = mysql_query($query);
	if ( !$result )
		die("Query for Pilots in table PILOTS failed.<br>");

	// Gather the records fields
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	$pilot_id      = $row["PILOT_ID"];
	$first_name    = $row["FIRST_NAME"];
	$last_name     = $row["LAST_NAME"];
	$email         = $row["EMAIL"];
	$rank_ac       = $row["RANK_AC"];
	$rank_helo     = $row["RANK_HELO"];
	$hours_ac      = $row["HOURS_AC"];
	$hours_helo    = $row["HOURS_HELO"];
	$hours_total   = $row["TOTAL_HRS"];
	$bid           = $row["BID"];
	$dart          = $row["DART"];
	$sar           = $row["SAR"];
	$fleet_support = $row["FLEET_SUP"];
	$loa           = $row["LOA"];
	$active        = $row["ACTIVE"];
	$joinDate      = $row["JOINED"];
	$lastFlyDate   = $row["LAST_FLY"];
	$deleted       = $row["DELETED"];
	$pilot_name    = $first_name ." " .$last_name;

	//
	// Build the HTML Form
	//

	echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
	echo "<html>\n";
	echo "<head>\n";
	echo "<title>APU - Edit Pilot Form</title>\n";
	echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n";
	echo "</head>\n";
	echo "<body bgcolor=\"#FFFFFF\" text=\"#000000\">\n";
	echo "<div align=\"center\">\n";
	  echo "<p><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"5\" color=\"#840000\"><b><font size=\"4\">APU Administration - Edit Pilot</font></b></font></p>\n";
	  echo "<table width=\"500\" border=\"0\" cellspacing=\"3\" cellpadding=\"1\">\n";
		echo "<tr bgcolor=\"#FFFFCC\">\n";
		  echo "<td><div align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"3\" color=\"#840000\"><b>" .$pilot_id ." - " .$pilot_name ."</b></font></div></td>\n";
		echo "</tr>\n";
	  echo "</table>\n";
	echo "</div>\n";
	echo "<table width=\"500\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\">\n";
	  echo "<tr>\n";
		echo "<td width=\"500\" height=\"256\" bgcolor=\"#FFFFCC\">\n";
		  echo "<form name=\"form1\" method=\"post\" action=\"pilot_update.php\">\n";
			echo "<table width=\"100%\" border=\"1\" cellspacing=\"1\" cellpadding=\"1\">\n";
			  echo "<tr>\n";
				echo "<td><div align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" color=\"#FF0000\"><b><font size=\"2\">* Reset Password:</font></b></font><input type=\"checkbox\" name=\"changepass\" value=\"1\"></div></td>\n";
				if ( $deleted )
					echo "<td><div align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" color=\"#FF0000\"><b><font size=\"2\">** Delete Pilot Record:</font></b></font><input type=\"checkbox\" name=\"delete_pilot\" value=\"1\" checked></div></td>\n";
				else
					echo "<td><div align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" color=\"#FF0000\"><b><font size=\"2\">** Delete Pilot Record:</font></b></font><input type=\"checkbox\" name=\"delete_pilot\" value=\"1\"></div></td>\n";
			  echo "</tr>\n";
			echo "</table>\n";
			echo "<br>\n";
			echo "<table width=\"100%\" border=\"0\" cellspacing=\"2\" cellpadding=\"1\">\n";
			  echo "<tr>\n";
				echo "<td><div align=\"right\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" color=\"#840000\"><b><font size=\"2\">First Name:</font></b></font></div></td>\n";
				echo "<td><input type=\"text\" name=\"firstname\" size=\"15\" maxlength=\"50\" value=\"" .$first_name ."\"></td>\n";
				echo "<td></td>\n";
			  echo "</tr>\n";
			  echo "<tr>\n";
				echo "<td><div align=\"right\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" color=\"#840000\"><b><font size=\"2\">Last Name:</font></b></font></div></td>\n";
				echo "<td><input type=\"text\" name=\"lastname\" size=\"15\" maxlength=\"50\" value=\"" .$last_name ."\"></td>\n";
				echo "<td></td>\n";
			  echo "</tr>\n";
			  echo "<tr>\n";
				echo "<td><div align=\"right\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" color=\"#840000\"><b><font size=\"2\">E-mail Address:</font></b></font></div></td>\n";
				echo "<td><input type=\"text\" name=\"email\" size=\"30\" maxlength=\"50\" value=\"" .$email ."\"></td>\n";
				echo "<td></td>\n";
			  echo "</tr>\n";
			  echo "<tr>\n";
				echo "<td><div align=\"right\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" color=\"#840000\"><b><font size=\"2\">Date Joined:</font></b></font></div></td>\n";
				echo "<td><input type=\"text\" name=\"joindate\" size=\"15\" maxlength=\"50\" value=\"" .$joinDate ."\"></td>\n";
				echo "<td><div align=\"right\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" color=\"#840000\" size=\"2\">Format YYYY-MM-DD</font></div></td>\n";
			  echo "</tr>\n";
			  echo "<tr>\n";
				echo "<td><div align=\"right\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" color=\"#840000\"><b><font size=\"2\">Last Fly Date:</font></b></font></div></td>\n";
				echo "<td><input type=\"text\" name=\"lastflydate\" size=\"15\" maxlength=\"50\" value=\"" .$lastFlyDate ."\"></td>\n";
				echo "<td><div align=\"right\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" color=\"#840000\" size=\"2\">Format YYYY-MM-DD</font></div></td>\n";
			  echo "</tr>\n";
			  echo "<tr>\n";
				echo "<td><div align=\"right\"><b><font color=\"#840000\" face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"2\">Aircraft Rank:</font></b></div></td>\n";
				echo "<td><input type=\"text\" name=\"ac_rank\" size=\"5\" maxlength=\"2\" value=\"" .$rank_ac ."\"></td>\n";
				echo "<td></td>\n";
			  echo "</tr>\n";
			  echo "<tr>\n";
				echo "<td><div align=\"right\"><b><font color=\"#840000\" face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"2\">Aircraft Hours:</font></b></div></td>\n";
				echo "<td><input type=\"text\" name=\"ac_hours\" size=\"15\" maxlength=\"7\" value=\"" .$hours_ac ."\"></td>\n";
				echo "<td></td>\n";
			  echo "</tr>\n";
			  echo "<tr>\n";
				echo "<td><div align=\"right\"><b><font color=\"#840000\" face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"2\">Helicopter Rank:</font></b></div></td>\n";
				echo "<td><input type=\"text\" name=\"helo_rank\" size=\"5\" maxlength=\"2\" value=\"" .$rank_helo ."\"></td>\n";
				echo "<td></td>\n";
			  echo "</tr>\n";
			  echo "<tr>\n";
				echo "<td><div align=\"right\"><b><font color=\"#840000\" face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"2\">Helicopter Hours:</font></b></div></td>\n";
				echo "<td><input type=\"text\" name=\"helo_hours\" size=\"15\" maxlength=\"7\" value=\"" .$hours_helo ."\"></td>\n";
				echo "<td></td>\n";
			  echo "</tr>\n";
			  echo "<tr>\n";
				echo "<td><div align=\"right\"><b><font color=\"#840000\" face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"2\">Total Hours:</font></b></div></td>\n";
				echo "<td><input type=\"text\" name=\"total_hours\" size=\"15\" maxlength=\"7\" value=\"" .$hours_total ."\"></td>\n";
				echo "<td></td>\n";
			  echo "</tr>\n";
			  echo "<tr>\n";
				echo "<td><div align=\"right\"><b><font color=\"#840000\" face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"2\">* Bid:</font></b></div></td>\n";
				echo "<td><input type=\"text\" name=\"bid\" size=\"30\" maxlength=\"100\" value=\"" .$bid ."\"></td>\n";
				echo "<td></td>\n";
			  echo "</tr>\n";
			echo "</table>\n";
			echo "<p align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" color=\"#0033FF\"><b><font size=\"2\">Additional Flight Activities</font></b></font></p>\n";
			echo "<table width=\"100%\" border=\"1\" cellspacing=\"1\" cellpadding=\"1\">\n";
			  echo "<tr>\n";
				echo "<td width=\"28%\"><div align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" color=\"#840000\"><b><font size=\"2\">DART</font></b></font></div></td>\n";
				echo "<td width=\"38%\"><div align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" color=\"#840000\"><b><font size=\"2\">SAR</font></b></font></div></td>\n";
				echo "<td width=\"34%\"><div align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" color=\"#840000\"><b><font size=\"2\">Fleet Support </font></b></font></div></td>\n";
			  echo "</tr>\n";
			  echo "<tr>\n";

			  if ( $dart )
				echo "<td width=\"28%\"><div align=\"center\"><input type=\"checkbox\" name=\"dart\" value=\"1\" checked></div></td>\n";
			  else
				echo "<td width=\"28%\"><div align=\"center\"><input type=\"checkbox\" name=\"dart\" value=\"1\"></div></td>\n";

			  if ( $sar )
				echo "<td width=\"38%\"><div align=\"center\"><input type=\"checkbox\" name=\"sar\" value=\"1\" checked></div></td>\n";
			  else
				echo "<td width=\"38%\"><div align=\"center\"><input type=\"checkbox\" name=\"sar\" value=\"1\"></div></td>\n";

			  if ( $fleet_support )
				echo "<td width=\"34%\"><div align=\"center\"><input type=\"checkbox\" name=\"fleet_support\" value=\"1\" checked></div></td>\n";
			  else
				echo "<td width=\"34%\"><div align=\"center\"><input type=\"checkbox\" name=\"fleet_support\" value=\"1\"></div></td>\n";

			  echo "</tr>\n";
			echo "</table>\n";

			echo "<p align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" color=\"#0033FF\"><b><font size=\"2\">Pilots Status</font></b></font></p>\n";
			echo "<table width=\"100%\" border=\"1\" cellspacing=\"1\" cellpadding=\"1\">\n";
			  echo "<tr>\n";
				echo "<td width=\"51%\"><div align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" color=\"#840000\"><b><font size=\"2\">Leave of Absence</font></b></font></div></td>\n";
				echo "<td width=\"49%\"><div align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" color=\"#840000\"><b><font size=\"2\">Active</font></b></font></div></td>\n";
			  echo "</tr>\n";
			  echo "<tr>\n";

			  if ( $loa )
				echo "<td width=\"51%\"><div align=\"center\"><input type=\"checkbox\" name=\"loa\" value=\"1\" checked></div></td>\n";
			  else
				echo "<td width=\"51%\"><div align=\"center\"><input type=\"checkbox\" name=\"loa\" value=\"1\"></div></td>\n";

			  if ( $active )
				echo "<td width=\"49%\"><div align=\"center\"><input type=\"checkbox\" name=\"active\" value=\"1\" checked></div></td>\n";
			  else
				echo "<td width=\"49%\"><div align=\"center\"><input type=\"checkbox\" name=\"active\" value=\"1\"></div></td>\n";

			  echo "</tr>\n";
			echo "</table>\n";
			echo "<p align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" color=\"#0033FF\"><b><font size=\"2\">Notes</font></b></font></p>\n";
			echo "<table width=\"100%\" border=\"0\" cellspacing=\"3\" cellpadding=\"1\">\n";
			  echo "<tr>\n";
				echo "<td width=\"21%\"><div align=\"right\"><font face=\"Verdana, Arial, Helvetica, sans-serif\"><b><font color=\"#FF0000\" size=\"1\">*</font></b></font></div></td>\n";
				echo "<td width=\"79%\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\">This will reset the pilots password to the default password.</font></td>\n";
			  echo "</tr>\n";
			  echo "<tr>\n";
				echo "<td width=\"21%\"><div align=\"right\"><font face=\"Verdana, Arial, Helvetica, sans-serif\"><b><font color=\"#FF0000\" size=\"1\">**</font></b></font></div></td>\n";
				echo "<td width=\"79%\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\">This will mark the pilots record as deleted.</font></td>\n";
			  echo "</tr>\n";
			  echo "<tr>\n";
				echo "<td width=\"21%\"><div align=\"right\"><font face=\"Verdana, Arial, Helvetica, sans-serif\"><b><font color=\"#840000\" size=\"1\">*</font></b></font></div></td>\n";
				echo "<td width=\"79%\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\">Some characters, such as # are not accepted by mySQL.</font></td>\n";
			  echo "</tr>\n";
			  echo "<tr>\n";
				echo "<td width=\"21%\"><div align=\"right\"><font face=\"Verdana, Arial, Helvetica, sans-serif\"><b><font color=\"#840000\" size=\"1\">Also</font></b></font></div></td>\n";
				echo "<td width=\"79%\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\">If updating hours, you must update total hours and rank.</font></td>\n";
			  echo "</tr>\n";
			  echo "<tr>\n";
				echo "<td width=\"21%\"><div align=\"right\"><font face=\"Verdana, Arial, Helvetica, sans-serif\"><b><font color=\"#840000\" size=\"1\"></font></b></font></div></td>\n";
				echo "<td width=\"79%\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\">Password Reset and Delete Pilot are seperate operations from the rest on this form.</font></td>\n";
			  echo "</tr>\n";
			echo "</table>\n";
			echo "<p align=\"center\">\n";
			  echo "<input type=\"hidden\" name=\"pilotid\" value=\"" .$pilot_id ."\">\n";
			  echo "<input type=\"submit\" name=\"Submit\" value=\"Submit\">\n";
			  echo "<br><br></p>\n";
		  echo "</form>\n";
		echo "</td>\n";
	  echo "</tr>\n";
	echo "</table>\n";

	//
	// Attempt connection to mySQL
	//

	$db = mysql_connect($dbhost, $dbuname, $dbpass)
		or die("Could not connect to the SQL server<br>");
	mysql_select_db($dbname);

	//
	// Select All Pirep for this Pilot
	//

	$pirepQuery  = "SELECT * FROM " .$table_prefix ."PIREP WHERE PILOT_ID = '" .$row["PILOT_ID"] ."' ORDER BY FLIGHT_DATE";
	$pireps = mysql_query($pirepQuery);
	if ( !$pireps )
		die("Query for current PIREP reports failed.<br>Click your browsers Refresh button to try again.<br>If you continue to receive this error, contact the webmaster<br>Failed with mySQL error: " . mysql_error());

	//
	// PIREP Table
	//

	echo "<br>\n";
	echo "<font face=\"Verdana, Arial, Helvetica, sans-serif\" color=\"#840000\" size=\"1\">\n";
	echo "<div align=\"center\">\n";
	  echo "<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n";
		echo "<tr>\n";
		  echo "<td>\n";
			echo "<div align=\"center\">\n";
			  echo "<p><font color=\"#840000\"><b>PIREP's for " .$pilot_id ." - " .$pilot_name ."</b></font></p>\n";
			  echo "<p><font size=\"2\" color=\"#000000\">Click on the given PIREP hyperlink to edit.</font></p>\n";
			  echo "<table width=\"99%\" border=\"1\" cellspacing=\"0\" cellpadding=\"1\">\n";
				// Header
				echo "<tr>\n";
				  echo "<td width=\"2%\"><div align=\"center\"><font size=\"1\"><b>#</b></font></div></td>\n";
				  echo "<td width=\"9%\"><div align=\"center\"><font size=\"1\"><b>Date</b></font></div></td>\n";
				  echo "<td width=\"6%\"><div align=\"center\"><font size=\"1\"><b>Type</b></font></div></td>\n";
				  echo "<td width=\"5%\"><div align=\"center\"><font size=\"1\"><b>Tail #</b></font></div></td>\n";
				  echo "<td width=\"5%\"><div align=\"center\"><font size=\"1\"><b>Flight #</b></font></div></td>\n";
				  echo "<td width=\"3%\"><div align=\"center\"><font size=\"1\"><b>Deprt</b></font></div></td>\n";
				  echo "<td width=\"3%\"><div align=\"center\"><font size=\"1\"><b>Dest</b></font></div></td>\n";
				  echo "<td width=\"3%\"><div align=\"center\"><font size=\"1\"><b>Time<br>Out</b></font></div></td>\n";
				  echo "<td width=\"3%\"><div align=\"center\"><font size=\"1\"><b>Time<br>In</b></font></div></td>\n";
				  echo "<td width=\"3%\"><div align=\"center\"><font size=\"1\"><b>Flight<br>Time</b></font></div></td>\n";
				  echo "<td width=\"4%\"><div align=\"center\"><font size=\"1\"><b>Fuel<br>Out</b></font> </div></td>\n";
				  echo "<td width=\"4%\"><div align=\"center\"><font size=\"1\"><b>Fuel<br>In </b></font></div></td>\n";
				  echo "<td width=\"4%\"><div align=\"center\"><font size=\"1\"><b>Fuel<br>Burn</b> </font></div></td>\n";
				  echo "<td width=\"2%\"><div align=\"center\"><font size=\"1\"><b>Pax</b></font></div></td>\n";
				  echo "<td width=\"6%\"><div align=\"center\"><font size=\"1\"><b>Cargo</b></font></div></td>\n";
				  echo "<td width=\"38%\"><div align=\"center\"><font size=\"1\"><b>Comments</b></font></div></td>\n";
				echo "</tr>\n";

				// PIREP Records
				while ($pirepRow = mysql_fetch_array($pireps, MYSQL_ASSOC))
				{
					// We only show total flight time here, with no regard for fixed or helo
					$flightTime = $pirepRow["TIME_FIXED"] + $pirepRow["TIME_HELO"];

					echo "<tr>\n";
						echo "<td width=\"2%\" height=\"16\"><div align=\"left\"><font size=\"1\"><a href=\"edit_pirep.php?pirep=" .$pirepRow["id"] ."\">" .$pirepRow["id"] ."</a></font></div></td>\n";
						echo "<td width=\"9%\" height=\"16\"><div align=\"center\"><font size=\"1\">" .$pirepRow["FLIGHT_DATE"] ."</font></div></td>\n";
						echo "<td width=\"6%\" height=\"16\"><div align=\"left\"><font size=\"1\">" .$pirepRow["AC_TYPE"] ."</font></div></td>\n";
						echo "<td width=\"5%\" height=\"16\"><div align=\"left\"><font size=\"1\">" .$pirepRow["TAIL_NUM"] ."</font></div></td>\n";
						echo "<td width=\"5%\" height=\"16\"><div align=\"left\"><font size=\"1\">" .$pirepRow["FLIGHT_NUM"] ."</font></div></td>\n";
						echo "<td width=\"3%\" height=\"16\"><div align=\"center\"><font size=\"1\">" .$pirepRow["DEPART"] ."</font></div></td>\n";
						echo "<td width=\"3%\" height=\"16\"><div align=\"center\"><font size=\"1\">" .$pirepRow["DEST"] ."</font></div></td>\n";
						echo "<td width=\"3%\" height=\"16\"><div align=\"center\"><font size=\"1\">" .$pirepRow["TIME_OUT"] ."</font></div></td>\n";
						echo "<td width=\"3%\" height=\"16\"><div align=\"center\"><font size=\"1\">" .$pirepRow["TIME_IN"] ."</font></div></td>\n";
						echo "<td width=\"3%\" height=\"16\"><div align=\"center\"><font size=\"1\">" .$flightTime ."</font></div></td>\n";
						echo "<td width=\"4%\" height=\"16\"><div align=\"right\"><font size=\"1\">" .$pirepRow["FUEL_OUT"] ."</font></div></td>\n";
						echo "<td width=\"4%\" height=\"16\"><div align=\"right\"><font size=\"1\">" .$pirepRow["FUEL_IN"] ."</font></div></td>\n";
						echo "<td width=\"4%\" height=\"16\"><div align=\"right\"><font size=\"1\">" .$pirepRow["FUEL_BURN"] ."</font></div></td>\n";
						echo "<td width=\"2%\" height=\"16\"><div align=\"right\"><font size=\"1\">" .$pirepRow["PAX"] ."</font></div></td>\n";
						echo "<td width=\"6%\" height=\"16\"><div align=\"right\"><font size=\"1\">" .$pirepRow["CARGO"] ."</font></div></td>\n";
						echo "<td width=\"38%\" height=\"16\"><div align=\"left\"><font size=\"1\">" .$pirepRow["COMMENTS"] ."</font></div></td>\n";
					echo "</tr>\n";
				}

			  echo "</table>\n";
			  echo "<br>\n";
			echo "</div>\n";
		  echo "</td>\n";
		echo "</tr>\n";
	  echo "</table>\n";
	echo "</div>\n";
	echo "</font>\n";

	//
	// End HTML
	//

    echo "<p align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\" color=\"#999999\">" .$APU_COPYRIGHT ."</font></p>\n";
	echo "</body>\n";
	echo "</html>\n";

?>
